local minimouYiji = fk.CreateSkill({
  name = "mini_mou__yiji",
})

Fk:loadTranslationTable{
  ["mini_mou__yiji"] = "遗计",

  [":mini_mou__yiji"] = "当你受到1点伤害后，你可以摸两张牌，然后你可以将至多等量张手牌交给任意名其他角色。<br/>" ..
  "当你每轮首次进入濒死状态时，你可以摸一张牌，然后你可将此牌交给一名其他角色。",

  ["$mini_mou__yiji1"] = "身不能征伐，此计或可襄君太平！",
  ["$mini_mou__yiji2"] = "此身赴黄泉，望明公见计如晤。",
}

minimouYiji:addEffect(fk.Damaged, {
  anim_type = "masochism",
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  on_use = function(self, event, target, player, data)
    ---@type string
    local skillName = minimouYiji.name
    local room = player.room
    player:drawCards(2, skillName)
    local toGive = player:getCardIds("h")
    if player.dead or #toGive == 0 then return end
    room:askToYiji(player, { cards = toGive, targets = room:getOtherPlayers(player, false), skill_name = skillName, min_num = 0, max_num = 2 })
  end
})

minimouYiji:addEffect(fk.EnterDying, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or target ~= player then return false end
    local room = player.room
    local logic = room.logic
    local dying_event = logic:getCurrentEvent():findParent(GameEvent.Dying, true)
    if dying_event == nil then return false end
    local mark = player:getMark("mini_mou__yiji-round")
    if mark == 0 then
      logic:getEventsOfScope(GameEvent.Dying, 1, function (e)
        local last_dying = e.data
        if last_dying.who == player then
          mark = e.id
          room:setPlayerMark(player, "mini_mou__yiji-round", mark)
          return true
        end
        return false
      end, Player.HistoryRound)
    end
    return mark == dying_event.id
  end,
  on_use = function(self, event, target, player, data)
    ---@type string
    local skillName = minimouYiji.name
    local room = player.room
    local toGive = player:drawCards(1, skillName)
    toGive = table.filter(toGive, function(id) return room:getCardArea(id) == Card.PlayerHand and room:getCardOwner(id) == player end)
    if player.dead or #toGive == 0 then return end
    room:askToYiji(player, { cards = toGive, targets = room:getOtherPlayers(player, false), skill_name = skillName, min_num = 0, max_num = 1 })
  end
})

return minimouYiji
